// @ts-ignore
import {href_handle} from "@/vue_plugin/router_auto.js";
// @ts-ignore
import {ajax} from "@/js/api_tool.js";
// @ts-ignore
import {cache, param_encode} from "@/js/s94_tool.js";

export interface TypeCartItem {
    link_id: number;
    title: string;
    head_img: string;
    price: number|string;
    sku_id?: number;
    additional_select?: object;
    num?: number;
    id?: number;
}

export interface TypeComputePrice {
    all_money: number;
    vip_money: number;
    promotion_money: number;
    additional_money: number;
    pay_money: number;
}

function cacheKeyItem(cart:S94Cart){
    return `cart_items_${cart.model_name}_${cart.link_table}_${cart.action}`;
}

export class S94Cart{
    model_name = '';
    action = '';
    link_table = '';
    cart_items:TypeCartItem[] = [];
    constructor(model_name='', action='', link_table='') {
        this.model_name = model_name;
        this.action = action;
        this.link_table = link_table;

        this.cart_items = [];
    }

    set_cache(){
        let cache_key = cacheKeyItem(this);
        cache({[cache_key]:this.cart_items});
        return this;
    }
    get_cache(){
        let cache_key = cacheKeyItem(this);
        let cart_items = cache(cache_key, []);
        this.cart_items = cart_items;
        return this;
    }

    to_index(timeout=0, is_replace=false){
        let cart_param = {model_name:this.model_name, link_table:this.link_table, action:this.action};
        let cart_param_str = param_encode(cart_param);
        href_handle(`/cart/index?${cart_param_str}`, timeout, is_replace);
    }

    to_buy(timeout=0, is_replace=false){
        this.set_cache();
        let cart_param = {model_name:this.model_name, link_table:this.link_table, action:this.action};
        let cart_param_str = param_encode(cart_param);
        href_handle(`/cart/info?${cart_param_str}`, timeout, is_replace);
    }

    add_item(cart:TypeCartItem){
        cart.num = cart.num || 1;
        cart.sku_id = cart.sku_id || 0;
        cart.additional_select = cart.additional_select || [];
        let key = JSON.stringify([cart.link_id, cart.sku_id, cart.additional_select]);
        let has = this.cart_items.some(row=>{
            let key_row = JSON.stringify([row.link_id, row.sku_id, row.additional_select]);
            if (key_row === key){
                // @ts-ignore
                row.num += cart.num;
                return true;
            }
            return false;
        })
        if (has) return this;
        this.cart_items.push({
            id: cart.id || 0,
            link_id: cart.link_id,
            sku_id: cart.sku_id,
            additional_select: cart.additional_select,
            num: cart.num,
            title: cart.title || "",
            price: cart.price || 0,
            head_img: cart.head_img || "",
        })
        return this;
    }

    remove_item(cart:{
        link_id: number;
        sku_id?: number;
        additional_select?: object;
        num?: number;
    }){
        cart.num = cart.num || 1;
        cart.sku_id = cart.sku_id || 0;
        cart.additional_select = cart.additional_select || [];
        let key = JSON.stringify([cart.link_id, cart.sku_id, cart.additional_select]);
        this.cart_items.some((row,i)=>{
            let key_row = JSON.stringify([row.link_id, row.sku_id, row.additional_select]);
            if (key_row === key){
                // @ts-ignore
                let num = row.num - cart.num;
                if (num <= 0){
                    this.cart_items.splice(i, 1);
                } else {
                    row.num = num;
                }
                return true;
            }
            return false;
        })
        return this;
    }


    async compute_price(): Promise<{compute_price_list: TypeComputePrice[], sum_data: TypeComputePrice}>
    {
        const items:any[] = [];
        this.cart_items.forEach((row)=>{
            if (row.link_id<=0 || !row.num) return ;
            items.push({
                model_name: this.model_name,
                link_table: this.link_table,
                link_id: row.link_id,
                num: row.num,
                sku_id: row.sku_id,
                additional_select: row.additional_select,
            })
        })

        let res = await ajax({
            url: '/home/cart/compute_price',
            data: {item: items},
            method: 'post'
        })

        let sum_data:TypeComputePrice = {
            all_money: 0,
            vip_money: 0,
            promotion_money: 0,
            additional_money: 0,
            pay_money: 0,
        }
        res.data.forEach((compute_price:TypeComputePrice)=>{
            Object.keys(sum_data).forEach(k=>{
                // @ts-ignore
                sum_data[k] += compute_price[k];
            })
        })
        let result:{
            compute_price_list: TypeComputePrice[];
            sum_data: TypeComputePrice;
        } = {
            compute_price_list: res.data,
            sum_data: sum_data
        };
        return result
    }
}





